import { TaDataUser } from "./user";
import { TaPost } from "../script/ajax";

// 搜索接口
export const TaDataActionSearch = {

    // 提交搜索记录
    set(keys: string = '') {
        if (TaDataUser.isLogin()) {
            TaPost('/shop/api.auth.action.search/set', { keys: keys })
        }
    },

    // 获取搜索记录
    get(): Promise<any> {
        return TaPost('/shop/api.auth.action.search/get')
    },

    // 获取热门词语
    hot(): Promise<any> {
        return TaPost('/shop/api.goods/hotkeys')
    },
};

// 用户收藏数据接口
export const TaDataActionCollect = {
    // 提交收藏记录
    set(gcode: string): Promise<any> {
        return TaPost('/shop/api.auth.action.collect/set', { gcode: gcode })
    },
    // 删除收藏记录
    del(gcode: string): Promise<any> {
        return TaPost('/shop/api.auth.action.collect/del', { gcode: gcode })
    },
    //  获取收藏记录
    get(data: any): Promise<any> {
        return TaPost('/shop/api.auth.action.collect/get', data)
    },
    // 清理收藏记录
    clear(): Promise<any> {
        return TaPost('/shop/api.auth.action.collect/clear')
    },
}

// 用户足迹数据接口
export const TaDataActionHistory = {
    //  提交访问记录
    set(gcode: string): Promise<any> {
        return TaPost('/shop/api.auth.action.history/set', { gcode: gcode })
    },
    // 删除访问记录
    del(gcode: string): Promise<any> {
        return TaPost('/shop/api.auth.action.history/del', { gcode: gcode })
    },
    // 获取访问记录
    get(data: any): Promise<any> {
        return TaPost('/shop/api.auth.action.history/get', data)
    },
    // 清理访问记录
    clear(): Promise<any> {
        return TaPost('/shop/api.auth.action.history/clear')
    },
}

// 其他扩展工具
export const TaUtil = {
    // 去空格
    trim(value: string) {
        return value.replace(/(^\s*)|(\s*$)/g, "");
    },
    // 内容替换
    replaceAll(text: string, repstr: string, newstr: string) {
        return text.replace(new RegExp(repstr, "gm"), newstr);
    },
    // 格式化手机号码
    mobile(num: string) {
        return num.length === 11 ? num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') : num;
    },
    // 金额格式化
    money(money: string) {
        return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
            /\,$/, '').split('').reverse().join('');
    }
}
